﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
#endregion

public partial class ucActorPermissionSearch : System.Web.UI.UserControl
{
    #region Fields
    UserManager _permissions = new UserManager();
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSearch.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearch.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnAdd.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAdd.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDeletePermission.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDeletePermission.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnDeletePermission.Attributes.Add("OnClick", "return confirmDelete()");
        if (!IsPostBack)
        {
            rblType.SelectedIndex = 0;
            Session["UserManager_Type"] = rblType.SelectedValue;
            Session["UserManager_ActorSearch"] = "Latest";
            Search();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ShowGridUsersRoles();
        if (rblType.SelectedIndex == -1)
        {
            Response.Write("<script>alert('Select Type !')</script>");
            return;
        }
        if (txtSearch.Text == string.Empty)
            Session["UserManager_ActorSearch"] = "BlankSearch";
        else
            Session["UserManager_ActorSearch"] = txtSearch.Text;
                
        Search();
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        Session["UserManager_Permission"] = "Modify";
        Session["UserManager_Control"] = "UserManager/ucActorPermissionAssign.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");           
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {            
            Session["UserManager_Permission"] = "Assign";
            Session["UserManager_Control"] = "UserManager/ucActorPermissionAssign.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx");
        }
    }
   
    protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowGridUsersRoles();
        Session["UserManager_Type"] = rblType.SelectedValue;
        Session["UserManager_ActorSearch"] = "Latest";
        Search();          
    }

    protected void gvwUsersRoles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }

    protected void btnDeletePermission_Click(object sender, EventArgs e)
    {
        Delete();
    }

    protected void gvwPermissions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwPermissions.PageIndex = e.NewPageIndex;
        SearchPermissions();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucAddPermission.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void gvwPermissions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }

    protected void gvwPermissions_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void gvwUsersRoles_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwUsersRoles.PageIndex = e.NewPageIndex;
        Search();
    }

    protected void lnbBack_Click(object sender, EventArgs e)
    {
        ShowGridUsersRoles();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ShowPermissions(object sender, EventArgs e)
    {
        LinkButton lnbGridUsersRoles = (LinkButton)sender;
        GridViewRow gvRow = (GridViewRow)lnbGridUsersRoles.NamingContainer;
        Session["ToSearch"] = gvRow.Cells[1].Text;

        if (SearchPermissions())
        {
            gvwPermissions.Visible = true;
            lnbBack.Visible = true;
            btnDeletePermission.Enabled = true;
            gvwPermissions.Caption = lnbGridUsersRoles.Text.ToUpper();
            gvwUsersRoles.Visible = false;
        }

    }
    #endregion

    #region Methods
    /// <summary>
    /// Display UserRoles
    /// </summary>
    void ShowGridUsersRoles()
    {
        btnDeletePermission.Enabled = false;
        gvwPermissions.Visible = false;
        lnbBack.Visible = false;
        gvwUsersRoles.Visible = true;
    }

    /// <summary>
    /// To serch UserRole
    /// </summary>
    void Search()
    {

        try
        {
            string toSearch = Session["UserManager_ActorSearch"].ToString();
            DataSet ds = new DataSet();

            if (!_permissions.SearchUsersRoles(Session["UserManager_Type"].ToString(), toSearch, out ds))
            {
                Response.Write("<script>alert('No Match Found !')</script>");
                return;
            }

            gvwUsersRoles.DataSource = ds;
            gvwUsersRoles.Columns[1].Visible = true;
            gvwUsersRoles.DataBind();
            gvwUsersRoles.Columns[1].Visible = false;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "User/Role Search Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    /// <summary>
    /// Search Permissions
    /// </summary>
    /// <returns></returns>
    bool SearchPermissions()
    {

        try
        {
            DataSet ds = new DataSet();

            if (!_permissions.SearchActorPermission(Session["UserManager_Type"].ToString(), Session["ToSearch"].ToString(), out ds))
            {
                Response.Write("<script>alert('No Permissions Assigned !')</script>");
                return false;
            }


            gvwPermissions.DataSource = ds;
            gvwPermissions.Columns[1].Visible = true;
            gvwPermissions.DataBind();
            gvwPermissions.Columns[1].Visible = false;

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "User/Role Permission Search Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
        return true;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    bool CheckSelection()
    {
        int count = 0, selectedRow = -1;

        for (int i = 0; i < gvwUsersRoles.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUsersRoles.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                count++;
                selectedRow = i;
            }
        }

        if (count == 0)
        {
            Response.Write("<script>alert('Select a User/Role')</script>");
            return false;
        }
        else if (count > 1)
        {
            Response.Write("<script>alert('Select Single User/Role')</script>");
            return false;
        }
        LinkButton lnbGridUsersRoles = (LinkButton)gvwUsersRoles.Rows[selectedRow].Cells[2].FindControl("lnbUsersRoles");
        Session["UserManager_User/RoleName"] = lnbGridUsersRoles.Text;
        Session["UserManager_User/RoleID"] = gvwUsersRoles.Rows[selectedRow].Cells[1].Text;
        return true;
    }

    /// <summary>
    /// Delete Permissions
    /// </summary>
    void Delete()
    {
        int index = 0;
        string actorID;
        string[] actorIDs = new string[gvwPermissions.Rows.Count];
        for (int i = 0; i < gvwPermissions.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwPermissions.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                actorIDs[index] = gvwPermissions.Rows[i].Cells[1].Text;
                index++;
            }
        }

        if (index == 0)
        {
            Response.Write("<script>alert('Select User/Role')</script>");
            return;
        }

        string condition = "PM_PMID='" + actorIDs[0] + "'";
        for (int i = 1; i < index; i++)
        {
            condition = condition + "or PM_PMID='" + actorIDs[i] + "'";
        }
        _permissions.RemovePermission(condition);
        Response.Write("<script>alert('Deleted Successfully !')</script>");
        SearchPermissions();
    }

   
    #endregion
}
